package kali.wpj.kali.common;

import android.content.Context;
import android.os.Bundle;
import android.os.Environment;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;

import com.github.johnpersano.supertoasts.SuperToast;

import java.io.File;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import kali.wpj.kali.R;
import kali.wpj.kali.model.TimelineAdapter;
import kali.wpj.kali.util.AnimationUtil;
import kali.wpj.kali.util.DateStyle;
import kali.wpj.kali.util.DateUtil;
import kali.wpj.kali.util.OpenFileIntent;
import kali.wpj.kali.util.SDCardUtils;

/**
 * Created by Server on 2015/5/9.
 */
public class NmapFragment extends Fragment {
    private ListView listView;
    List<String> data;
    private String path;
    private TimelineAdapter timelineAdapter;
    private List<File> mFileList;
    private List<File> fileList;
    private List<String> yearList;
    private List<String> monthList;
    private List<String> titleList;
    private List<String> addressList;
    private Context context;
    private String TAG = "NmapFragment";

    public NmapFragment(Context context) {
        this.context = context;
    }

    @Override
    public View onCreateView(LayoutInflater inflater,
                             @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.activity_listview, container, false);
        listView = (ListView) view.findViewById(R.id.listview);
        listView.setDividerHeight(0);
        path = SDCardUtils.getSDCardPath() + "/TimeFSafe";
        mFileList = new ArrayList<File>();
        timelineAdapter = new TimelineAdapter(context, getData());
        listView.setLayoutAnimation(AnimationUtil.getListAnimTranslate());
        listView.setAdapter(timelineAdapter);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                startActivity(OpenFileIntent.getHtmlByBrowerIntent(((TextView) view.findViewById(R.id.address)).getText().toString()));

            }
        });
        return view;
    }

    private List<Map<String, Object>> getData() {
        List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();


        File path = new File(getSdDir());

        fileList = getFile(path);

        for (int i = 0; i < fileList.size(); i++) {
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("year", yearList.get(i));
            map.put("month", monthList.get(i));
            map.put("title", titleList.get(i));
            map.put("address", addressList.get(i));
            list.add(map);
        }

        return list;
    }
    //获取Sd根路径

    public String getSdDir() {
        File sdDir = null;

        if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
            sdDir = android.os.Environment.getExternalStorageDirectory();
        }
        return sdDir.toString() + "/TimeFSafe";
    }

    //获取Sd卡根目录下文件集合
    public List<File> getFile(File file) {
        yearList = new ArrayList<String>();
        monthList = new ArrayList<String>();
        titleList = new ArrayList<String>();
        addressList = new ArrayList<String>();
        File[] fileArray = file.listFiles();
        int i = 0;
        for (File f : fileArray) {
            if (f.isFile()) {

                System.out.println("filename：" + f.getName() + f.getAbsolutePath());
                if (f.getName().contains("html")) {
                    mFileList.add(f);
                    Log.i(TAG, f.getName() + "----");
                    String tem = f.getName().substring(6);
                    Log.i(TAG, tem);
                    yearList.add(tem.substring(0, 4));
                    System.out.println(tem.substring(0, tem.indexOf(".")));
                    String tem1 = tem.substring(0, tem.indexOf("."));
                    System.out.println(tem1 + "-----------");

                    String date = DateUtil.StringToString(tem1, DateStyle.MM_DD_HH_MM_SS);
                    System.out.println("日期:" + date);
                    monthList.add(date);
                    addressList.add(f.getAbsolutePath());
                    i++;
                    titleList.add("端口扫描[" + i + "]");
                }
            } else {
                getFile(f);
            }
        }
        if(mFileList.size()==0){
            SuperToast superToast = new SuperToast(context);
            superToast.setDuration(SuperToast.Duration.SHORT);
            superToast.setText(getString(R.string.norec));
            superToast.setIcon(SuperToast.Icon.Dark.INFO, SuperToast.IconPosition.LEFT);
            superToast.show();
        }
        return mFileList;
    }


}
